package com.lvchen.common.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.lvchen.R;
import com.lvchen.common.MyApplication;
import com.lvchen.common.utils.ConnectityUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


/**
 * 
 * @author joe.xiao
 *
 * @Date 2015年9月29日
 *
 * @Email xiaobenqiao027@163.com
 *
 * @Description
 */
public abstract class BaseFragment extends Fragment {

	protected Context mContext;
	protected BaseActivity mActivity;
	protected View mContentView;

	protected Map<String, BaseFragment> mCachedFragments = new HashMap<String, BaseFragment>();
	protected EmptyView mEmptyView;

	//标题栏右侧为Text的点击事件的flag
	private int RTextListenerFlag = 1;
	//标题栏右侧为imag的点击事件的flag
	private int RImgListenerFlag = 2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActivity = (BaseActivity) getActivity();
		mContext = MyApplication.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (mEmptyView == null) {
			mEmptyView = new EmptyView(mContext, inflater, new OnClickListener() {
				@Override
				public void onClick(View v) {
					// 重新加载
					doRetryAction();
				}
			});
		}
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	protected void setNoNetworkView() {
		if (!ConnectityUtils.isNetworkConnected(mContext) && mEmptyView != null) {
			mEmptyView.setNoNetWork();
			setEmptyView(mEmptyView.getView());
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onPause() {
		super.onPause();

	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}





	/**
	 * @Description 刷新一下
	 *
	 */
	protected void doRetryAction() {

	}


	// 该方法在baseActivity中也存在
	public void setEmptyView(View view) {
		if (mContentView == null && getContentView() == null) {
			return;
		}
		LinearLayout parent = (LinearLayout) mContentView.findViewById(R.id.loadableListHolder);
		if (parent == null) {
			RuntimeException e = new NullPointerException("Please include empty_loading.xml to your content view!");
			return;
		}

		View cv = parent.getChildAt(0);
		if (null != cv) {
			cv.setVisibility(View.GONE);
		}
		if (parent.getChildCount() > 1) {
			parent.removeViewAt(1);
		}
		if (view != null) {
			ViewGroup parentViewGroup = (ViewGroup) view.getParent();
			if (null != parentViewGroup) {
				parentViewGroup.removeView(view);
			}
			parent.addView(view);
		}
	}

	protected abstract View getContentView();

	protected void replaceFragment(int viewId, Fragment fragment, String tag, boolean isBack) {
		try {
			FragmentTransaction ft = getChildFragmentManager().beginTransaction();
			ft.replace(viewId, fragment, tag);
			ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			if (isBack) {
				ft.addToBackStack(tag);
			}
			if (!mCachedFragments.containsKey(tag)) {
				mCachedFragments.put(tag, (BaseFragment) fragment);
			}
			ft.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加 Fragment
	 * 
	 * @param id
	 *            布局资源id
	 * @param fragment
	 */
	public void addFragment(int id, Fragment fragment, String tag, boolean isBack) {
		FragmentTransaction ft = getChildFragmentManager().beginTransaction();
		if (fragment.isAdded()) {
			ft.remove(fragment);
		}
		ft.add(id, fragment, tag);
		if (isBack) {
			ft.addToBackStack(tag);
		}

		ft.commit();
	}

	protected BaseFragment getCachedFragment(String key) {
		if (mCachedFragments.containsKey(key)) {
			return mCachedFragments.get(key);
		}
		return null;
	}

	protected void putCachedFragment(String key, BaseFragment fragment) {
		if (!mCachedFragments.containsKey(key)) {
			mCachedFragments.put(key, fragment);
		}
	}

	protected void clearCachedFragments() {
		mCachedFragments.clear();
	}

	protected void removeCachedFragment(String key) {
		if (mCachedFragments.containsKey(key)) {
			mCachedFragments.remove(key);
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		try {
			Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
			childFragmentManager.setAccessible(true);
			childFragmentManager.set(this, null);

		} catch (NoSuchFieldException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}

}
